﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wolfbyte.Core.UnitOfWork;
using Wolfbyte.Core.Repositories;
using System.Data.Linq;

namespace Wolfbyte.UnitOfWork.LinqToSql
{
    class LinqToSqlUnitOfWork : IUnitOfWork
    {
        private DataContext DataContext;
        private bool started = false;

        public bool Started
        {
            get
            {
                return started;
            }
        }

        public LinqToSqlUnitOfWork(DataContext DataContext)
        {
            this.DataContext = DataContext;
        }

        #region IUnitOfWork Members

        public IRepository<T> Repository<T>() where T : class
        {
            return new LinqToSqlRepository<T>(DataContext);
        }

        public void Save()
        {
            DataContext.SubmitChanges();
        }

        public void Start()
        {
            started = true;
        }

        public void Stop()
        {
            started = false;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (DataContext != null)
                DataContext.Dispose();
            DataContext = null;
        }

        #endregion
    }
}
